﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WanPiJie.Datas
{
	public class BabyData
	{
		public Baby AddBaby(int userId, string name, DateTime birthday, string gender)
		{
			Baby baby = new Baby();
			baby.UserId = userId;
			baby.Name = name;
			baby.Birthday = birthday;
			baby.Gender = gender;
			using (WanPiJieEntities db = new WanPiJieEntities())
			{
				db.BabySet.AddObject(baby);
				db.SaveChanges();
			}
			return baby;
		}

		public Baby GetBaby(int id)
		{
			using (WanPiJieEntities db = new WanPiJieEntities())
			{
				return db.BabySet.SingleOrDefault(p => p.Id == id);
			}
		}

		public List<Baby> GetBabies(int userId)
		{
			using (WanPiJieEntities db = new WanPiJieEntities())
			{
				return db.BabySet.Where(p => p.UserId == userId).ToList();
			}
		}

		public void UpdateBabyName(int id, string name)
		{
			using (WanPiJieEntities db = new WanPiJieEntities())
			{
				var baby = this.GetBaby(id);
				if (baby != null)
				{
					baby.Name = name;
					db.SaveChanges();
				}
				else
				{

				}
			}
		}
	}
}
